<?php
require_once 'db.php';

class DbStat{
		public $date;
		public $para;

		function get($date=null){
			global $db;
			if($date == null)
				return false;
			$query = $db->select('dbstats',array('TIME_STAMP'=>$date));
			if($query){
				$rs = $db->db_fetch_array($query);
				$this->date = $rs['DATE'];
				$this->para = array(
					'PCT_FREE_MEM'=>$rs['PCT_FREE_MEM'],
					'DIC_HR'=>$rs['DIC_HR'],
					'PCT_RELOADS'=>$rs['PCT_RELOADS'],
					'BUFFER_HR'=>$rs['BUFFER_HR'],
					'REDO_WAIT_R'=>$rs['REDO_WAIT_R'],
					'IMM_ALLOC'=>$rs['IMM_ALLOC'],
					'IMM_COPY'=>$rs['IMM_COPY'],
					'WAIT_ALLOC'=>$rs['WAIT_ALLOC'],
					'WAIT_COPY'=>$rs['WAIT_COPY'],
					'PCT_SORTS'=>$rs['PCT_SORTS'],
					'ROLLBACK'=>$rs['ROLLBACK']
					);
			}
			else
				return false;
		}
		
		function DbState($date=null){
			$this->get($date);
		}
		
		function getDuration($start, $end) {
			global $db;
			$q = "select * from dbstats where TIME_STAMP between TO_DATE($start, 'YYYYMMDDHH24MISS') and TO_DATE($end, 'YYYYMMDDHH24MISS')";
			$query = $db->db_query($q);
			$db->execute($query);
			
			$rs = array();
			
			while($row = oci_fetch_assoc($query)) {
				$rs[] = $row;
			}
			
			return $rs;
		}

		/**
		 * must get the user first to remove the user
		 */
		function remove(){
			global $db;
			return $db->remove('dbstats',array('TIME_STAMP'=>$this->$date));
		}
		function add(){
			global $db;
			
			$query = $db->db_query("select systimestamp from dual");
			$db->execute($query);
			$row = oci_fetch_assoc($query);
			var_dump($row);
			echo 'current date: '.$row['SYSTIMESTAMP'].'<br />';
			
			$update = array(
				'PCT_FREE_MEM'=>$this->para['PCT_FREE_MEM'],
				'DIC_HR'=>$this->para['DIC_HR'],
				'PCT_RELOADS'=>$this->para['PCT_RELOADS'],
				'BUFFER_HR'=>$this->para['BUFFER_HR'],
				'REDO_WAIT_R'=>$this->para['REDO_WAIT_R'],
				'IMM_ALLOC'=>$this->para['IMM_ALLOC'],
				'IMM_COPY'=>$this->para['IMM_COPY'],
				'WAIT_ALLOC'=>$this->para['WAIT_ALLOC'],
				'WAIT_COPY'=>$this->para['WAIT_COPY'],
				'PCT_SORTS'=>$this->para['PCT_SORTS'],
				'ROLLBACK'=>$this->para['ROLLBACK']
			);
			
			$sql = "INSERT INTO dbstats ";
			$temp_field = array();
			$temp_val = array();
			$temp_field[] = 'TIME_STAMP';
			foreach($update as $key=>$val){
				$temp_field[] = $key;
				$temp_val[] = "'$val'";
			}
			$field = implode(',', $temp_field);
			$val = implode(',', $temp_val);
			$sql = $sql."($field) VALUES (SYSTIMESTAMP, $val)";
			//echo $sql;
			$result = $db->db_query($sql);
			//var_dump($sql);
			
			return oci_execute($result);
		}

	}
?>
